package com.pan.controller.Notices;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.Methods;
import com.pan.controller.tools.R1;
import com.pan.service.INoticeService;
import com.pan.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notices")
@Tag(name = "NoticeController",description = "公告操作类")
public class NoticeController {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IUserService userService;

    Methods methods = new Methods();

    //获取公告，获取到的会是 delete 为 1 的公告
    @Operation(summary = "获取所有公告",description = "普通用户只能查看到未删除的公告")
    @GetMapping
    @MyLog(title = "获取公告，delete为1的",optParam = "无",businessType = BusinessType.SELECT)
    public R1 getAllNotice(){
        return new R1(true,noticeService.getNotDeleteNotice());
    }

    //根据id获取公告, delete 为 1
    @Operation(summary = "按id查找公告",description = "普通用户只能查看到未删除的公告")
    @GetMapping("/{id}")
    @MyLog(title = "根据id获取公告, delete 为 1",optParam = "无",businessType = BusinessType.SELECT)
    public R1 getNoticeById(@Parameter(description = "编号",required = true,example = "1") @PathVariable(value = "id") Integer id){
        return new R1(true,noticeService.getByIdE(id));
    }


    //操作员逻辑删除公告
    @Operation(summary = "逻辑删除公告",description = "'1'为未删除 '0'为已删除")
    @PutMapping
    @MyLog(title = "操作员逻辑删除公告",optParam = "#{id}",businessType = BusinessType.DELETE)
    public R1 Logicdelete(@Parameter(description = "编号",required = true,example = "1") @RequestParam Integer id,
                          HttpServletRequest request){
        //检查用户权限
        if (methods.isYanfa(request,userService)){
            return new R1(noticeService.LogicDelete(id));
        }
        return new R1(false,"权限不足");
    }

}
